package com.waitingsnow.modules.shell.view.baseView
{
import com.waitingsnow.core.debug.Logger;
import com.waitingsnow.core.model.config.AppConfig;
import com.waitingsnow.core.util.display.DisplayUtil;
import com.waitingsnow.core.util.ds.ExternalUtil;
import com.waitingsnow.core.util.ds.StringUtil;

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.clearInterval;
import flash.utils.setInterval;

/**
 * 
 * @author waitingsnow
 * $Id: CountDownView.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class CountDownView extends Sprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>CountDownView</code> instance.
	 * 
	 */
	public function CountDownView()
	{
		initDisplay();
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var time_tf:TextField;
	private var countTime_tf:TextField;
	private var countTicket_tf:TextField;
	private var timeStr:String = "{0}天{1}小时{2}分{3}秒";
	private var ticketStr:String = "{0}张";
	private var _countTime:Number;
	private var _ticketNum:uint;
	private var intervalID:uint;
	
	//==========================================================================
	//  Methods
	//==========================================================================
	private function initDisplay():void
	{
		time_tf = DisplayUtil.getTf("演唱会倒计时", 0, 0, 150, 32);
		addChild(time_tf);
		var ticket_tf:TextField = DisplayUtil.getTf("剩余票数", 0, 40, 130, 32);
		addChild(ticket_tf);
		countTime_tf = DisplayUtil.getTf("", 150, 0, 250, 32);
		addChild(countTime_tf);
		countTicket_tf = DisplayUtil.getTf("", 150, 40, 220, 32);
		addChild(countTicket_tf);
		var tft:TextFormat = new TextFormat("宋体", 20, 0x808080, true);
		time_tf.defaultTextFormat = countTime_tf.defaultTextFormat = countTicket_tf.defaultTextFormat = tft;
		time_tf.setTextFormat(tft);
		ticket_tf.setTextFormat(tft);
		
		ExternalUtil.callback(AppConfig.UPDATE_LEFT_TICKET_TO_SWF, updateLeftTicketToSwf);
	}
	
	private function updateCountTime(countTime:Number):void
	{
		countTime = countTime/1000;
		var day:int;
		var hour:int;
		var min:int;
		var sec:int;
		if (countTime <= 0)
		{
			day = hour = min = sec = 0;
		}
		else
		{
			day = int(countTime/(3600*24));
			hour = int((countTime%(3600*24))/(3600));
			min = int(countTime%(3600)/60);
			sec = int(countTime%(60));
		}
		countTime_tf.text = StringUtil.substitute(timeStr, day, hour, min, sec);
	}
	
	public function updateTicketNum(ticketNum:uint):void
	{
		countTicket_tf && (countTicket_tf.text = StringUtil.substitute(ticketStr, ticketNum));
	}
	
	public function setCountData(countTime:Number, ticketNum:uint, 
								 beginFlag:Boolean = false, hasTicket:Boolean = false):void
	{
		_countTime = countTime;
		_ticketNum = ticketNum;
		updateTicketNum(_ticketNum);
		if ((countTime < 0) && !beginFlag)
		{
			countTime_tf.visible = false;
			time_tf.text = "演出方因故延迟直播，请耐心等待！";
			time_tf.width = 450;
			time_tf.x -= 20;
		}
		else
		{
			time_tf.text = beginFlag ? "正在直播" : "演唱会倒计时";
			if (beginFlag)
			{
				beginTimeHandler();
				intervalID = setInterval(beginTimeHandler, 1000);
			}
			else
			{
				updateCountTime(_countTime);
				intervalID = setInterval(countTimeHandler, 1000);
			}
		}
	}
	
	public function dispose():void
	{
		parent && parent.removeChild(this);
	}
	
	private function showStartTime(countTime:Number):void
	{
		var hour:int = int(countTime/3600);
		var min:int = int(countTime%3600/60);
		var sec:int = int(countTime%60);
		var msg:String = getDateString(hour) + ":" + getDateString(min) + ":" + getDateString(sec);
		countTime_tf.text = msg;
	}
	
	private function getDateString(time:Number):String
	{
		var timeStr:String = (time > 9) ? String(time) : ("0" + time);
		return timeStr;
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function countTimeHandler():void
	{
		_countTime -= 1000;
		if (_countTime <= 0)
		{
			clearInterval(intervalID);
		}
		updateCountTime(_countTime);
	}
	
	private function beginTimeHandler():void
	{
		_countTime += 1000;
		showStartTime(_countTime/1000);
	}
	
	private function updateLeftTicketToSwf(val:int):void
	{
		updateTicketNum(val);
	}
}
}